#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
模板匹配应用程序 - 主窗口UI定义
使用PyQt5实现的模板匹配应用程序UI
"""

from PyQt5.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel,
                             QComboBox, QGridLayout, QGroupBox, QSplitter, QTabWidget)
from PyQt5.QtCore import Qt
from image_view import ImageView, TemplateView

class Ui_MainWindow:
    """主窗口UI类"""
    
    def setupUi(self, MainWindow):
        """设置UI元素"""
        # 设置主窗口大小
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1000, 600)
        
        # 创建中央窗口部件
        self.centralwidget = QWidget(MainWindow)
        MainWindow.setCentralWidget(self.centralwidget)
        
        # 创建主布局（水平）
        self.main_layout = QHBoxLayout(self.centralwidget)
        
        # 创建左右拆分窗口
        self.splitter = QSplitter(Qt.Horizontal)
        
        # 创建左侧面板用于显示源图像/结果
        self.left_panel = QWidget()
        self.left_layout = QVBoxLayout(self.left_panel)
        self.left_panel.setLayout(self.left_layout)
        
        # 创建右侧面板用于显示模板和控制按钮
        self.right_panel = QWidget()
        self.right_layout = QVBoxLayout(self.right_panel)
        self.right_panel.setLayout(self.right_layout)
        
        # 创建标签页用于源图像/结果和热图
        self.result_tabs = QTabWidget()
        
        # 添加图像视图
        self.image_view = ImageView()
        self.image_view.setMinimumSize(600, 400)
        self.result_tabs.addTab(self.image_view, "源图像/结果")
        
        # 添加热图视图
        self.heatmap_view = ImageView()
        self.heatmap_view.setMinimumSize(600, 400)
        self.heatmap_view.enable_selection = False  # 禁用热图上的选择功能
        self.result_tabs.addTab(self.heatmap_view, "匹配热图")
        
        # 将标签页添加到左侧布局
        self.left_layout.addWidget(self.result_tabs)
        
        # 创建模板组
        self.template_group = QGroupBox("模板图像")
        template_layout = QVBoxLayout()
        
        # 添加模板视图
        self.template_view = TemplateView()
        self.template_view.setMinimumSize(300, 200)
        template_layout.addWidget(self.template_view)
        
        self.template_group.setLayout(template_layout)
        self.right_layout.addWidget(self.template_group)
        
        # 添加按钮面板
        self.button_panel = QWidget()
        self.button_layout = QVBoxLayout(self.button_panel)
        
        # 创建控制组
        self.control_group = QGroupBox("操作")
        control_grid = QGridLayout()
        
        # 创建各个操作按钮
        self.btn_load_image = QPushButton("加载图像")
        self.btn_load_template = QPushButton("加载模板")
        self.btn_draw_template = QPushButton("绘制模板")
        self.btn_save_template = QPushButton("保存模板")
        self.btn_match = QPushButton("执行匹配")
        
        # 添加匹配方法选择
        method_label = QLabel("匹配方法:")
        self.method_combo = QComboBox()
        self.method_combo.addItems([
            "平方差匹配法 (SqDiff)",
            "归一化平方差匹配法 (SqDiffNormed)",
            "相关匹配法 (CCorr)",
            "归一化相关匹配法 (CCorrNormed)",
            "相关系数匹配法 (CCoeff)",
            "归一化相关系数匹配法 (CCoeffNormed)"
        ])
        self.method_combo.setCurrentIndex(5)  # 默认选择归一化相关系数匹配法
        
        # 设置按钮尺寸策略
        button_height = 30
        for btn in [self.btn_load_image, self.btn_load_template, 
                   self.btn_draw_template, self.btn_save_template, self.btn_match]:
            btn.setMinimumHeight(button_height)
        
        # 按网格布局添加按钮
        control_grid.addWidget(self.btn_load_image, 0, 0)
        control_grid.addWidget(self.btn_load_template, 0, 1)
        control_grid.addWidget(self.btn_draw_template, 1, 0)
        control_grid.addWidget(self.btn_save_template, 1, 1)
        control_grid.addWidget(method_label, 2, 0)
        control_grid.addWidget(self.method_combo, 2, 1)
        control_grid.addWidget(self.btn_match, 3, 0, 1, 2)  # 跨两列
        
        self.control_group.setLayout(control_grid)
        self.button_layout.addWidget(self.control_group)
        
        # 添加一些弹性空间
        self.button_layout.addStretch()
        
        # 将按钮面板添加到右侧布局
        self.right_layout.addWidget(self.button_panel)
        
        # 添加结果信息显示区域
        self.result_group = QGroupBox("匹配结果")
        self.result_layout = QVBoxLayout()
        self.result_label = QLabel("未执行匹配")
        self.result_label.setStyleSheet("font-family: Consolas, Courier New, monospace; font-size: 9pt;")
        self.result_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
        self.result_label.setWordWrap(True)
        self.result_layout.addWidget(self.result_label)
        self.result_group.setLayout(self.result_layout)
        self.button_layout.addWidget(self.result_group)
        
        # 将左右面板添加到分割器
        self.splitter.addWidget(self.left_panel)
        self.splitter.addWidget(self.right_panel)
        self.splitter.setSizes([700, 300])  # 初始分割比例
        
        # 将分割器添加到主布局
        self.main_layout.addWidget(self.splitter)
        
        # 添加状态栏
        self.statusbar = MainWindow.statusBar()
        
        # 设置一些样式
        MainWindow.setStyleSheet("""
            QLabel { font-weight: bold; }
            QPushButton { 
                background-color: #4a86e8; 
                color: white; 
                border-radius: 4px;
                padding: 6px;
            }
            QPushButton:hover { 
                background-color: #3a76d8; 
            }
            QPushButton:pressed { 
                background-color: #2a66c8; 
            }
        """)
